package org.wmq.skyserver.handler;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.wmq.skycommon.constant.MessageConstant;
import org.wmq.skycommon.pojo.entity.Result;

import java.sql.SQLIntegrityConstraintViolationException;

@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler
    public Result exceptionHandle(SQLIntegrityConstraintViolationException exception){
        String message = exception.getMessage();
        if (message.contains("Duplicate entry")){
            String[] slice = message.split(" ");
            return Result.error(slice[2] + MessageConstant.ALREDAY_EXIST);
        }
        return Result.error(MessageConstant.UNKNOW_ERROR);
    }
}
